<?php
namespace Addons\UserMessageSet\Controller\Admin;
use Lib\Response;
use Session;
use Models\BaseModel as Model;

class Messageset extends \BaseController
{
	//列表展示以及修改显示或者隐藏的基本信息
	public function index(){
		$wid = \Session::get("wid");
		$message = (new Model("qy_messageset"))->find(["wid"=>$wid]);
		$fields = (new Model("qy_custom_fields"))->where(["wid"=>$wid])->order('orders asc')->list_all();
		$this->assign("fields",$fields);
		$this->assign("count",count($fields));
		$this->assign("message",$message);
		return $this->show();
	}

	public function upd_messageset(){
		$wid = \Session::get("wid");
		$message = (new Model("qy_messageset"))->find(["wid"=>$wid]);
		if(is_post()){
			setApi();
			if(!$message->id){
				$message = new Model("qy_messageset");
				$message->wid = $wid;
			}
			$messag = $_POST["messag"];
			$messags = $_POST["messags"];
			foreach ($messag as $key => $value) {
				$message->$value = 1;
			}
			foreach ($messags as $keys => $values) {
				$message->$values = 0;
			}
			if($message->save()){
				sendSuccess("修改成功");
			}
			sendError("修改失败");
		}
		$this->assign("message",$message);
		return $this->show();
	}

	//添加自定义字段
	public function add_custom_fields(){
		if(is_post()){
			setApi();
			$wid = \Session::get("wid");
			$title = $_POST["title"];
			if($title == ""){
				sendError("字段名称不能为空");
			}
			$is_title = (new Model("qy_custom_fields"))->find(["wid"=>$wid,"title"=>$title]);
			if($is_title->id){
				sendError("字段名称已存在");
			}
			$content = json_encode($_POST["content"],320);
			$status = $_POST["status"];
			$count = (new Model("qy_custom_fields"))->where(["wid"=>$wid])->count();
			$fields = new Model("qy_custom_fields");
			$fields->wid = $wid;
			$fields->title = $title;
			$fields->content = $content;
			$fields->status = $status;
			$fields->orders = $count+1;
			$fields->ctime = time();
			if($fields->save()){
				sendSuccess("添加成功");
			}
			sendError("添加失败");
		}
		return $this->show();
	}

	//删除自定义字段
	public function del_fields(){
		setApi();
		$id = $_POST["id"];
		$fields = (new Model("qy_custom_fields"))->find($id);
		$fields_list = (new Model("qy_custom_fields"))->where(["orders@>"=>$fields->orders])->list_all();
		foreach ($fields_list as $key => $value) {
			$fields_list[$key]->orders = $fields_list[$key]->orders - 1;
			$fields_list[$key]->save();
		}
		if($fields->remove()){
			sendSuccess("删除成功");
		}
		sendError("删除失败");
	}
	//上移
	public function shift_up(){
		setApi();
		$wid = \Session::get("wid");
		$id = $_POST["id"];
		$fields = (new Model("qy_custom_fields"))->find($id);
		$fieldsone = (new Model("qy_custom_fields"))->find(["wid"=>$wid,"orders"=>$fields->orders-1]);
		$fieldsone->orders = $fieldsone->orders+1;
		$fields->orders = $fields->orders-1;
		if($fieldsone->save() && $fields->save()){
			sendSuccess("移动成功");
		}
		sendError("移动失败");
	}
	//下移
	public function shift_below(){
		setApi();
		$wid = \Session::get("wid");
		$id = $_POST["id"];
		$fields = (new Model("qy_custom_fields"))->find($id);
		$fieldsone = (new Model("qy_custom_fields"))->find(["wid"=>$wid,"orders"=>$fields->orders+1]);
		$fieldsone->orders = $fieldsone->orders-1;
		$fields->orders = $fields->orders+1;
		if($fieldsone->save() && $fields->save()){
			sendSuccess("移动成功");
		}
		sendError("移动失败");
	}

	//修改自定义字段
	public function upd_fields_data(){
		setApi();
		$id = \Request::get("id");
		$fields = (new Model("qy_custom_fields"))->find($id);
		if($fields->status == 2){
			$fields->content = json_decode($fields->content,1);
		}
		$this->assign("fields",$fields);
		return $this->show();
	}

	public function upd_fields(){
		setApi();
		$wid = \Session::get("wid");
		$id = $_POST["id"];
		$fields = (new Model("qy_custom_fields"))->find($id);
		$content = json_encode($_POST["content"],1);
		$title = $_POST["title"];
		$is_title = (new Model("qy_custom_fields"))->find(["wid"=>$wid,"title"=>$title]);
		if($fields->title != $title && $is_title->id){
			sendError("字段名称已存在");
		}
		$fields->title = $title;
		$fields->content = $content;
		if($fields->save()){
			sendSuccess("修改成功");
		}
		sendError("修改失败");
	}

}
?>